package com.taokatao.cardmee;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.taokatao.cardmee.business.MainActivity;
import com.taokatao.cardmee.db.upgrade.CardStoreDBDataImporter;
import com.taokatao.cardmee.db.model.User;
import com.taokatao.cardmee.R;
/**
 * 应用启动入口
 */
public class LauncherActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawableResource(android.R.color.white);

        // http://blog.csdn.net/love100628/article/details/43238135
        if (!this.isTaskRoot()) {
            // 判断该Activity是不是任务空间的源Activity，“非”也就是说是被系统重新实例化出来
            // 如果你就放在launcher Activity中话，这里可以直接return了
            Intent mainIntent = getIntent();
            String action = mainIntent.getAction();
            if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)) {
                finish();
                return; // finish()之后该活动会继续执行后面的代码，你可以logCat验证，加return避免可能的exception
            }
        }

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancelAll();

        ImageView view = new ImageView(this);
        view.setScaleType(ImageView.ScaleType.FIT_XY);
        Glide.with(LauncherActivity.this).load(R.mipmap.launcher_cover).centerCrop().into(view);
        setContentView(view);

        initDatabase();
        initSpfProperties();
        initLastUser();

        view.postDelayed(new Runnable() {
            @Override
            public void run() {
                boolean isFirstUse = true;
                int lastSplashVersion = Utils.spReadInt(C.sp.SETTINGS_SPLASH_VERSION, -1);
                if (lastSplashVersion >= Utils.getAppVersionCode()) {
                    isFirstUse = false;
                }

                if (isFirstUse) {
                    Intent intent = new Intent(LauncherActivity.this, SplashActivity.class);
                    startActivity(intent);
                    ActivityCollector.finishAllExcept(SplashActivity.class);
                } else {
                    Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
                    startActivity(intent);
                    ActivityCollector.finishAllExcept(MainActivity.class);
                }
            }
        }, 1600);
    }

    private void initDatabase() {
        CardStoreDBDataImporter.importData();
    }

    // 初始化 sp 相关应用参数
    private void initSpfProperties() {

    }

    // 初始化 用户信息
    private void initLastUser() {
        User lastUser = App.i().getCurrUser();
        if (lastUser != null) {
            Utils.logd("LauncherActivity lastUser find out:" + lastUser.getUuid());
        } else {
            Utils.logd("LauncherActivity first use");
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.activity_fade_in, R.anim.activity_scale_exit);
    }
}